package com.wanxi.dangdang.common;

import java.io.Serializable;

/**
 * @author 王锐
 * 前后端交互数据标准
 */
public class CommonResult<T> implements Serializable {
    private static final long serialVersionUID = 1L;

    /**
     * 成功标志
     */
    private boolean success;

    /**
     * 描述
     */
    private String message;

    /**
     * 返回代码
     */
    private Integer code;

    /**
     * 时间戳
     */
    private long timestamp = System.currentTimeMillis();

    /**
     * 结果对象
     */
    private T data;

    public CommonResult() {
    }

    public CommonResult(Integer code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }
    public CommonResult(Boolean aBoolean,Integer code, String message, T data) {
        this.success= aBoolean;
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public boolean isSuccess() {
        return success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public long getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(long timestamp) {
        this.timestamp = timestamp;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    private void setResultCode(ResultCode resultCode){
        this.message = resultCode.getMessage();
        this.code = resultCode.getCode();
    }

    //返回成功
    public static CommonResult success(){
        CommonResult commonResult = new CommonResult();
        commonResult.setSuccess(true);
        commonResult.setResultCode(ResultCode.SUCCESS);
        return commonResult;
    }
    //返回成功
    public static CommonResult success(Object data){
        CommonResult commonResult = new CommonResult();
        commonResult.setSuccess(true);
        commonResult.setResultCode(ResultCode.SUCCESS);
        commonResult.setData(data);
        return commonResult;
    }
    //返回成功
    public static CommonResult success(String message, Object data){
        CommonResult commonResult = new CommonResult();
        commonResult.setResultCode(ResultCode.SUCCESS);
        commonResult.setMessage(message);
        commonResult.setSuccess(true);
        commonResult.setData(data);
        return commonResult;
    }

    //返回成功
    public static CommonResult success(String message, int code , Object data){
        CommonResult commonResult = new CommonResult();
        commonResult.setCode(code);
        commonResult.setMessage(message);
        commonResult.setSuccess(true);
        commonResult.setData(data);
        return commonResult;
    }

    //返回失败
    public static CommonResult failure(ResultCode resultCode){
        CommonResult commonResult = new CommonResult();
        commonResult.setSuccess(false);
        commonResult.setResultCode(resultCode);
        return commonResult;
    }


    //返回失败
    public static CommonResult failure(int code, String message){
        CommonResult commonResult = new CommonResult();
        commonResult.setSuccess(false);
        commonResult.setCode(code);
        commonResult.setMessage(message);
        return commonResult;
    }
    //返回失败
    public static CommonResult failure(ResultCode resultCode, Object data){
        CommonResult commonResult = new CommonResult();
        commonResult.setSuccess(false);
        commonResult.setResultCode(resultCode);
        commonResult.setData(data);
        return commonResult;
    }
    //返回失败
    public static CommonResult failure(String message, Object data){
        CommonResult commonResult = new CommonResult();
        commonResult.setMessage(message);
        commonResult.setSuccess(false);
        commonResult.setData(data);
        return commonResult;
    }

    //返回失败
    public static CommonResult failure(int code, Object data){
        CommonResult commonResult = new CommonResult();
        commonResult.setCode(code);
        commonResult.setSuccess(false);
        commonResult.setData(data);
        return commonResult;
    }

    /**
     * 成功返回结果
     *
     * @param data 获取的数据
     */
    public static <T> CommonResult<T> successData(T data) {
        return new CommonResult<T>(true,ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMessage(), data);
    }

    /**
     * 成功返回结果
     *
     * @param data 获取的数据
     * @param  message 提示信息
     */
    public static <T> CommonResult<T> success(T data, String message) {
        return new CommonResult<T>(ResultCode.SUCCESS.getCode(), message, data);
    }

    /**
     * 失败返回结果
     * @param message 提示信息
     */
    public static <T> CommonResult<T> failed(String message) {
        return new CommonResult<T>(ResultCode.FAILED.getCode(), message, null);
    }

    /**
     * 参数验证失败返回结果
     */
    public static <T> CommonResult<T> validateFailed() {
        return failed(ResultCode.VALIDATE_FAILED.getMessage());
    }

    /**
     * 参数验证失败返回结果
     * @param message 提示信息
     */
    public static <T> CommonResult<T> validateFailed(String message) {
        return new CommonResult<T>(ResultCode.VALIDATE_FAILED.getCode(), message, null);
    }

    /**
     * 未登录返回结果
     */
    public static <T> CommonResult<T> unauthorized(T data) {
        CommonResult result = new CommonResult();
        result.setCode(ResultCode.UNAUTHORIZED.getCode());
        result.setMessage(ResultCode.UNAUTHORIZED.getMessage());
        result.setData(data);
        return result;
    }

    /**
     * 未授权返回结果
     */
    public static <T> CommonResult<T> forbidden(T data) {
        CommonResult result = new CommonResult();
        result.setCode(ResultCode.FORBIDDEN.getCode());
        result.setMessage(ResultCode.FORBIDDEN.getMessage());
        result.setData(data);
        return result;
    }
}
